<?php

/**
 * 计划任务管理
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class TasksAction extends AdminbaseAction {

    public $refreshtype;

    function _initialize() {
        parent::_initialize();
        //刷新任务类型
        $this->refreshtype = array(
            1 => "刷新首页",
            2 => "刷新栏目页",
            3 => "刷新自定义页面",
        );
        //每月
        $this->doday = array(0 => "*不限制", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31);
        //每周
        $this->doweek = array(0 => "*不限制", 1 => "星期一", 2 => "星期二", 3 => "星期三", 4 => "星期四", 5 => "星期五", 6 => "星期六", 7 => "星期日");
        //几点
        $this->dohour = array(0 => "*不限制", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
        //分钟
        $this->dominute = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59);
    }

    /**
     * 计划任务 
     */
    public function index() {
        $db = M("Task");
        $count = $db->count();
        $page = $this->page($count, 20);
        $data = $db->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        $this->assign("data", $data);
        $this->assign("Page", $page->show('Admin'));
        $this->display();
    }

    /**
     * 添加计划任务 
     */
    public function add() {
        if ($this->isPost()) {
            $db = M("Task");
            if(isset($_POST['dominute'])){
                $_POST['dominute'] = implode(',',$_POST['dominute']);
            }
            if ($data = $db->create()) {
                if ($db->add($data)) {
                    $this->success("添加计划任务成功！", U("Tasks/index"));
                } else {
                    $this->error("添加计划任务失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            import('@.ORG.Util.Form');
            $this->assign("doday", Form::select($this->doday, '', 'name="doday"'));
            $this->assign("doweek", Form::select($this->doweek, '', 'name="doweek"'));
            $this->assign("dohour", Form::select($this->dohour, '', 'name="dohour"'));
            $this->assign("dominute", Form::select($this->dominute, '', 'name="dominute[]" size="12" style="width:350px;" multiple'));
            $this->display();
        }
    }
    
    /**
     * 计划任务编辑 
     */
    public function edit(){
        if ($this->isPost()) {
            $db = M("Task");
            if(isset($_POST['dominute'])){
                $_POST['dominute'] = implode(',',$_POST['dominute']);
            }
            if ($data = $db->create()) {
                if ($db->save($data)) {
                    $this->success("编辑计划任务成功！", U("Tasks/index"));
                } else {
                    $this->error("编辑计划任务失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            import('@.ORG.Util.Form');
            $id = $this->_get('id');
            if(!$id){
                $this->error("参数错误！");
            }
            $db = M("Task");
            $data = $db->where(array("id"=>$id))->find();
            
            $this->assign("doday", Form::select($this->doday, $data['doday'], 'name="doday"'));
            $this->assign("doweek", Form::select($this->doweek, $data['doweek'], 'name="doweek"'));
            $this->assign("dohour", Form::select($this->dohour, $data['dohour'], 'name="dohour"'));
            $this->assign("dominute", Form::select($this->dominute, $data['dominute'], 'name="dominute[]" size="12" style="width:350px;" multiple'));
            $this->assign("info",$data);
            $this->display();
        }
    }
    
    /**
     * 删除计划任务 
     */
    public function delete(){
        $id = $this->_get("id");
        if (!$id) {
            $this->error("参数不完整！");
        }
        $db = M("Task");
        $status = $db->where(array("id" => $id))->delete();
        if ($status) {
            $this->success("删除计划任务成功！");
        } else {
            $this->error("删除计划任务失败！");
        }
    }

    /**
     * 刷新任务 
     */
    public function refresh() {
        $db = M("Gentasks");
        $count = $db->count();
        $page = $this->page($count, 20);
        $data = $db->limit($page->firstRow . ',' . $page->listRows)->order(array("gid" => "DESC"))->select();
        $this->assign("data", $data);
        $this->assign("Page", $page->show('Admin'));
        $this->display();
    }

    /**
     * 添加刷新任务 
     */
    public function refreshadd() {
        if ($this->isPost()) {
            $db = M("Gentasks");
            if (isset($_POST['catid'])) {
                $_POST['catid'] = implode(',', $_POST['catid']);
            }
            if ($data = $db->create()) {
                if ($db->add($data)) {
                    $this->success("添加刷新任务成功！", U("Tasks/refresh"));
                } else {
                    $this->error("添加刷新任务失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            import('@.ORG.Util.Form');
            $typeid = $this->_get('typeid');
            if (empty($typeid)) {
                $typeid = 1;
            }
            if ((int) $typeid == 1) {
                $categorydata = '<select name="catid[]" size="16" multiple="" style="width:350px;" id="catid"></select>';
                $typename = "";
            }
            if ((int) $typeid == 2) {
                import('@.ORG.Util.Tree');
                $tree = new Tree();
                //栏目列表 可以用缓存的方式
                $array = F("Category");
                foreach ($array as $catid => $r) {
                    if ($r['type'] == 2 && $r['child'] == 0)
                        continue;

                    if ($r['child'] == 0) {
                        if (!$r['ishtml'])
                            unset($array[$catid]);
                            continue;
                    }
                    $array[$catid] = $r;
                }
                $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
                $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
                $str = "<option value='\$catid' \$selected \$disabled>\$spacer \$catname</option>";
                $tree->init($array);
                $categorydata = '<select name="catid[]" size="16" multiple="" style="width:350px;" id="catid">' . $tree->get_tree(0, $str, $parentid) . '</select>';
                $typename = "栏目";
            }
            if ((int) $typeid == 3) {
                $db = M('Customtemp');
                $array = $db->order(array("tempid" => "DESC"))->getField("tempid,name");
                $categorydata = Form::select($array, '', 'name="catid[]" size="16" multiple="" style="width:350px;" id="catid"');
                $typename = "自定义页面";
            }


            $refreshtype = Form::select($this->refreshtype, $typeid, 'name="type" size="8" style="width:350px;" onChange="self.location.href=\'?a=refreshadd&m=Tasks&g=Tasks&typeid=\'+this.options[this.selectedIndex].value;"');

            $this->assign("refreshtype", $refreshtype);
            $this->assign('typename', $typename);
            $this->assign("catid", $categorydata);
            $this->display();
        }
    }

    /**
     * 编辑刷新任务 
     */
    public function refreshedit() {
        if ($this->isPost()) {
            $db = M("Gentasks");
            if (isset($_POST['catid'])) {
                $_POST['catid'] = implode(',', $_POST['catid']);
            }
            if ($data = $db->create()) {
                if ($db->save($data)) {
                    $this->success("编辑刷新任务成功！", U("Tasks/refresh"));
                } else {
                    $this->error("编辑刷新任务失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            import('@.ORG.Util.Form');
            $typeid = $this->_get('typeid');
            $gid = $this->_get("gid");
            if (!$typeid || !$gid) {
                $this->error("参数不完整！");
            }
            $db = M("Gentasks");
            $data = $db->where(array("gid" => $gid))->find();
            if ((int) $typeid == 1) {
                $categorydata = '<select name="catid[]" size="16" multiple="" style="width:350px;" id="catid"></select>';
                $typename = "";
            }
            if ((int) $typeid == 2) {
                import('@.ORG.Util.Tree');
                $tree = new Tree();
                //栏目列表 可以用缓存的方式
                $array = F("Category");
                $catidarray = explode(",", $data['catid']);
                foreach ($array as $catid => $r) {
                    if ($r['type'] == 2 && $r['child'] == 0)
                        continue;

                    if ($r['child'] == 0) {
                        if (!$r['ishtml']){
                            unset($array[$catid]);
                            continue;
                        }
                    }
                    if (in_array($catid, $catidarray)) {
                        $r['selected'] = 'selected';
                    }
                    $array[$catid] = $r;
                }
                
                $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
                $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
                $str = "<option value='\$catid' \$selected \$disabled>\$spacer \$catname</option>";
                $tree->init($array);
                $categorydata = '<select name="catid[]" size="16" multiple="" style="width:350px;" id="catid">' . $tree->get_tree(0, $str, $parentid) . '</select>';
                $typename = "栏目";
            }
            if ((int) $typeid == 3) {
                $db = M('Customtemp');
                $array = $db->order(array("tempid" => "DESC"))->getField("tempid,name");
                $categorydata = Form::select($array, $data['catid'], 'name="catid[]" size="16" multiple="" style="width:350px;" id="catid"');
                $typename = "自定义页面";
            }


            $refreshtype = Form::select($this->refreshtype, $typeid, 'name="type" size="8" style="width:350px;" onChange="self.location.href=\'?a=refreshadd&m=Tasks&g=Tasks&typeid=\'+this.options[this.selectedIndex].value;"');

            $this->assign("refreshtype", $refreshtype);
            $this->assign($data);
            $this->assign('typename', $typename);
            $this->assign("catid", $categorydata);
            $this->display();
        }
    }

    /**
     * 删除刷新任务 
     */
    public function refreshdelete() {
        $gid = $this->_get("gid");
        if (!$gid) {
            $this->error("参数不完整！");
        }
        $db = M("Gentasks");
        $status = $db->where(array("gid" => $gid))->delete();
        if ($status) {
            $this->success("删除刷新任务成功！");
        } else {
            $this->error("删除刷新任务失败！");
        }
    }

}

?>
